<?php

namespace App\Http\Controllers\Admin;


use App\Models\ResourceType;
use App\Validate\ResourceTypeValidate;
use Illuminate\Support\Facades\DB;

/**
 * 数字资源总数据大类型管理（对接对外资源）（不能添加，系统内定）
 */
class ResourceTypeController extends CommonController
{
    public $model = null;
    public $validate = null;

    public function __construct()
    {
        parent::__construct();

        $this->validate = new ResourceTypeValidate();
        $this->model = new ResourceType();
    }

    /**
     * 类型(用于下拉框选择)
     */
    public function filterList()
    {

        $condition[] = ['is_del', '=', 1];

        return $this->model->getFilterList(['id', 'type_name'], $condition, 'id asc');
    }

    /**
     * 获取数字资源大类型
     * @param keywords 检索条件
     * @param page int 当前页
     * @param limit int 分页大小
     */
    public function lists()
    {
        $limit = $this->request->input('limit', 10);
        $page = $this->request->input('page', 1);
        $res = $this->model->lists($this->request->all());
        if (empty($res['data'])) {
            return $this->returnApi(203, '暂无数据');
        }
        //增加序号
        $res['data'] = $this->addSerialNumber($res['data'], $page, $limit);
        $res = $this->disPageData($res);
        return $this->returnApi(200, '获取成功', true, $res);
    }

    /**
     * 详情
     * @param id int 类型id
     */
    public function detail()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('detail')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        $res = $this->model->select('id', 'type_name', 'img', 'intro', 'create_time', 'number', 'is_show_new', 'is_show_read_list')->find($this->request->id);

        if (!$res) {
            return $this->returnApi(201, "参数传递错误");
        }

        return $this->returnApi(200, "获取成功", true, $res->toArray());
    }

    /**
     * 新增
     * @param type_name string 类型名称
     * @param img string 图片
     * @param intro string 简介
     */
    public function add()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('add')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        $is_exists = $this->model->nameIsExists($this->request->type_name, 'type_name');

        if ($is_exists) {
            return $this->returnApi(202, "该名称已存在");
        }

        $res = $this->model->add($this->request->all());

        if (!$res) {
            return $this->returnApi(202, "新增失败");
        }
        return $this->returnApi(200, "新增成功", true);
    }

    /**
     * 修改
     * @param id int 类型id
     * @param type_name string 类型名称
     * @param img string 图片
     * @param intro string 简介
     */
    public function change()
    {

        //增加验证场景进行验证
        if (!$this->validate->scene('change')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        $is_exists = $this->model->nameIsExists($this->request->type_name, 'type_name', $this->request->id);

        if ($is_exists) {
            return $this->returnApi(202, "该名称已存在");
        }

        $res = $this->model->change($this->request->all());

        if (!$res) {
            return $this->returnApi(202, "修改失败");
        }
        return $this->returnApi(200, "修改成功", true);
    }

    /**
     * 删除
     * @param id int 类型id
     */
    public function del()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('del')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        $res = $this->model->del($this->request->id);

        if ($res === true) {
            return $this->returnApi(200, "删除成功", true);
        }
        return $this->returnApi(202, $res);
    }


    /**
     * 隐藏 和 显示(是否显示最新上架)
     * @param id int 活动id
     * @param is_show_new int 是否显示最新上架  1 显示  2 不显示
     */
    public function cancelAndReleaseNew()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('cancel_and_release_new')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        DB::beginTransaction();
        try {
            list($code, $msg) = $this->model->resumeAndCancel($this->request->id, 'is_show_new', $this->request->is_show_new);

            $is_show_new = $this->request->is_show_new == 1 ? '显示' : '隐藏';

            DB::commit();
            return $this->returnApi($code, $is_show_new . $msg, $code === 200 ? true : false);
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->returnApi(202, $e->getMessage());
        }
    }


    /**
     * 隐藏 和 显示(是否显示阅读榜单)
     * @param id int 活动id
     * @param is_show_read_list int 是否显示阅读榜单  1 显示  2 不显示
     */
    public function cancelAndReleaseReadList()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('cancel_and_release_read_list')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        DB::beginTransaction();
        try {
            list($code, $msg) = $this->model->resumeAndCancel($this->request->id, 'is_show_read_list', $this->request->is_show_read_list);

            $is_show_read_list = $this->request->is_show_read_list == 1 ? '显示' : '隐藏';

            DB::commit();
            return $this->returnApi($code, $is_show_read_list . $msg, $code === 200 ? true : false);
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->returnApi(202, $e->getMessage());
        }
    }
}
